import { getuserhouse, deluserhouse, getcommunity } from "../../utils/api"
import { setStorageSyncSecond, getStorageSyncTime } from "../../utils/storage"
import '../../utils/lodash_fix'
let _ = require("../../utils/lodash.min");
const to = require('../../utils/jump')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    houselist: [],
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    let a = getStorageSyncTime('community')
    if (a) {
      getcommunity().then(res => {
        if (_.isEqual(res.statusCode, 200)) {
          setStorageSyncSecond('community', res.data.data, 10)
          this.getuserhouse()
        }
      })
    } else {
      this.getuserhouse()
    }
    this.setData({ houselist: wx.getStorageSync('houselist') })
  },
  getuserhouse() {
    getuserhouse().then(res => {
      console.log(res)
      if (_.isEqual(res.statusCode, 200)) {
        let arr = wx.getStorageSync('community')
        let list = res.data.data
        arr.forEach(e => {
          list.forEach((v) => {
            if (e.uuid == v.community_uuid) {
              v.community_name = e.community_name
            }
          })
        })
        setStorageSyncSecond('houselist', list, 10)
        this.setData({ houselist: list })
      } else {
        wx.showToast({
          title: res.data.msg,
          icon: 'none'
        })
      }
    })
  },
  delhouse(e) {
    let house_uuid = e.currentTarget.dataset.house_uuid
    wx.showModal({
      title: '解绑',
      content: '您确定解除该房屋的绑定吗？',
      success: (res) => {
        if (res.confirm) {
          deluserhouse({ house_uuid }).then(res => {
            if (_.isEqual(res.statusCode, 200)) {
              wx.showToast({
                title: '解绑成功',
              })
              this.getuserhouse()
            } else {
              wx.showToast({
                title: '解绑失败',
                icon: 'error'
              })
            }
          })
        }
      }
    })

  },
  tobinding() {
    to('/pages/binding/binding')
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})